/*
 * @Author: ChunLai
 * @Date: 2023-07-25 09:48:14
 * @LastEditTime: 2025-08-27 14:49:53
 * @Description:页面公共数据配置
 * @FilePath: \02.bldinsure\src\store\modules\page.js
 */
import pageDits from "@/config/page.js";
import { yuLuBirthOrder } from "@/methods/date";

const state = {
  /* ===== 首页 ===== */
  // 条款
  colligateFiles: [
    {
      fileName: "保险条款",
      keyword: "条款",
      value: "tk",
    },
    {
      fileName: "费率表",
      keyword: "费率",
      value: "flb",
    },
    {
      fileName: "产品说明书",
      keyword: "产品说明",
      value: "cpsms",
    },
    {
      fileName: "免责条款",
      keyword: "免除",
      value: "mianze",
    },
  ],
  // 投保前置页 - 客户告知弹窗内容配置
  productNoticesList: [],
  comPreNotice: [], //首页必读公共文件
  comClauseList: [], // 保司公共文件(利安)
  tabClauses: [], // html
  // 通用理赔(接口没有返回使用)
  lipeicontent: "",
  // 万能跳转的险种
  omnipotentNaire: [], //问卷(百年)
  omnipApiNaire: "0", //接口问卷(长城)非
  omnipApiNaireEx: [], //接口问卷(长城)非
  omnipotentAccount: [], //关联账户(百年)
  baosiHtml: "", // 详情图下特殊文本配置(利安)
  dutysConfig: [], // 特殊责任配置(利安)
  plansConfig: [], // 计划配置

  /* ===== 投保前确认 ===== */
  shuangLuAge: 60,
  presureCfg: [],
  tgForInsureLink: [], // 配置无权限，推广员可直接投保的（不走客服模式）
  tgNoneInsuAgent: [], // 配置无权限，推广员可直接投保的（特殊人员走客服模式）
  insurChannelConfig: null, // 1:有权限的提示阻断(阳光)
  h5JumpConfig: null, // h5对接(国联)
  /**
   * pros: 产品
   * h5Link： 链接
   * shareType：有权限分享类型 1海报-民盛 2海报-保司 3民盛直投 4保司直投
   */
  /* ===== 客户信息输入 ===== */
  // 1.用户信息填写规则
  nameMaxLength: 30, // 姓名校验最大长度
  isFiristBill: "0", // 首单(长城00555)0表示首年,1表示非首年
  firstBillRelateCode: [], // 首单关联产品
  occuDisLevelMin: 10,
  occuDisLevelArr: [],
  // 税收文件填写,默认为下拉选择
  hasResidentFile: false,
  revenueFileType: "1", // 税收文件填写类型(1 长城; 2复兴; 3东吴/合众)
  // 隔代年龄差距(百年为17)
  ageInterval: 18,
  minorAge: 18, // 被保人未成年年龄
  // 4.是否有接口校验地址操作,默认没有
  hasApiCheckAddr: false,
  // 受益人顺位+个数
  benefyOrderMax: 3, // 顺位
  benefyNumMax: 3, // 每个顺位添加最大值
  fieldDescConfig: {}, // 字段描述配置
  limitFeeObj: {}, // 银行卡限额数据
  riskRelateOccup: [], // 产品关联配置不同职业码表
  // 需要校验投保区域的险种
  insuAreaCalibration: [],
  //需要校验的地址 postalAreaName homeAreaName
  calibrationArea: ["areaName"],
  bbrMinNum: 1, // 最少
  bbrMaxNum: 1, // 最多
  syrMinNum: 1,
  /* ===== 险种计划 ===== */
  preInputFlag: {}, // 预录单信息配置
  isPrerecordOrder: false,
  preRecordOrderMsg: "",
  birthyBillAge: 0, // 生日件试算年龄

  /* ===== 告知 ===== */
  gzPopRead: "0", //首页后台配置必读文件
  healthyLists: [],
  preNotice: false, // 前置告知
  autoHealthyApi: false, // 智能告知接口
  riskRelateNotice: [], //特殊产品配置不同告知(长城00555)
  minorCustodyCertificate: false, // 未成年人监护证明(利安)
  // 互联网简易告知
  riskCheckNotice: "0", //1是
  isNetInsure: "2", //1复星互联网；2长险；3网销(安盛)
  bbrAutoHealthApi: "2", // 1部分否，可智能核保，否则，阻断
  noticeConfigType: "0",

  /* ===== 影像资料 ===== */
  // 1.自拍照(to原selfie)
  selfiePhotos: {
    show: false,
    isCheck: false, // 是否校验完成
    hasTbr: false,
    hasBbr: false,
  },
  // 2.病历影像件
  medicalPhotos: {
    isShow: false,
    isCheck: false,
    maxCount: 50,
    title: "拍摄健康或财务资料",
  },
  // 3.受益人
  bnfPhotos: {
    isShow: true,
    isCheck: true,
  },
  // 4.关系证明---特殊地区编码(合众)
  specialEntity: [],
  bankPhoto: "1",

  /* ===== 保单确认 ===== */
  showCashTable: false, // 展示现金价值
  checkCashRead: false, // 是否校验必读
  doubRecordAbutment: false, // 保司是否对接双录(展示信息)
  doubRecordForbid: [],
  dRecordPassFlag: "0", // 双录质检结果(接口查询)
  doubleRecordEwmApi: false, // 是否调用双录二维码展示接口
  drEwmImage: "", //双录二维码
  fxqCount: "0", // 反洗钱计算
  /* === 签名 === */
  dlrDontSign: false, // 代理人不需要签字
  interLinkSign: false, //(百年,接口返回签字链接)
  singleFileSign: false, //(百年,单个文件签字)
  fileSignUls: false, //(需要签名的文件,非接口返回)
  transcribeDesc: "", // 抄录说明
  transcribeText: "", // 抄录文本
  transcribeProducts: [], // 所需产品
  /* === 支付 === */
  ocrFaceVerify: false, // 签名人脸识别开关
  dontRepalceBank: [], // 不可修改银行卡(配置缴费方式)
  dontChangePayMode: false, // 不可修改银行卡的支付方式
  /* === 结果页 === */
  revisitBtnLink: [""], // 显示回访按钮连接
  revisitDomain: "",
  ruleWarn: "",
  /* === type3安盛 === */
  insuYear: {}, // 生效日期
  renewal: {}, // 续保
  // PdfReader强制切换模式
  clickReadMode: "",
  noticeMode: "1",
  /**标体新模块*/
  targetsModalName: "标的信息",
};

const getters = {
  pageConfigs: (state) => state,
  gzPopRead: (state) => state.gzPopRead,
  colligateFiles: (state) => state.colligateFiles,
  lipeicontent: (state) => state.lipeicontent,
  productNoticesList: (state) => state.productNoticesList,
  comPreNotice: (state) => state.comPreNotice,
  homeMustReadList: (state) => {
    let res = [];
    if (state.productNoticesList.length > 0) {
      res = [...state.productNoticesList];
    }
    if (state.comPreNotice.length > 0) {
      if (res.length > 0) {
        state.comPreNotice.forEach((e) => {
          const fd2Obj = res.find((r) => r.titleName == e.titleName);
          if (!fd2Obj && e.content) {
            res.push(e);
          }
        });
      } else {
        res = [...state.comPreNotice];
      }
    }
    res = res.filter((i) => i.content);
    return res;
  },
  tabClauses: (state) => state.tabClauses,
  comClauseList: (state) => state.comClauseList,
  pdfWords: (state) => {
    let res = [];
    if (state.tabClauses.length > 0) {
      res = [...state.tabClauses];
    }
    if (state.comClauseList.length > 0) {
      res = [...res, ...state.comClauseList];
    }
    return res;
  },
  omnipotentNaire: (state) => state.omnipotentNaire,
  omnipApiNaire: (state) => state.omnipApiNaire,
  omnipApiNaireEx: (state) => state.omnipApiNaireEx,
  omnipotentAccount: (state) => state.omnipotentAccount,
  shuangLuAge: (state) => state.shuangLuAge,

  presureCfg: (state) => state.presureCfg,
  baosiHtml: (state) => state.baosiHtml,
  zeRenList: (state, getters, rootState) => {
    let arr = [];
    const apiArr = JSON.parse(JSON.stringify(rootState.varietyInfo.dutys));
    if (apiArr.length > 0) {
      arr = apiArr.sort((a, b) => a.id - b.id); // 排序
    }
    if (state.dutysConfig.length > 0) {
      arr = state.dutysConfig;
    }
    return arr;
  },
  insurePlansList: (state) => {
    let arr = [];
    if (state.plansConfig.length > 0) {
      arr = state.plansConfig;
    }
    return arr;
  },
  insurChannelConfig: (state) => state.insurChannelConfig,
  h5JumpConfig: (state) => state.h5JumpConfig,
  // h5跳转链接
  h5InsuLink: (state) => {
    let res = "";
    if (state.h5JumpConfig && state.h5JumpConfig.h5Link) {
      res = state.h5JumpConfig.h5Link;
    }
    return res;
  },
  // h5分享方式
  h5InsuShareType: (state) => {
    let res = "";
    if (state.h5JumpConfig && state.h5JumpConfig.shareType) {
      res = state.h5JumpConfig.shareType;
    }
    return res;
  },
  bbrMinNum: (state) => state.bbrMinNum,
  bbrMaxNum: (state) => state.bbrMaxNum,
  syrMinNum: (state) => state.syrMinNum,
  minorAge: (state) => state.minorAge,
  occuDisLevelMin: (state) => state.occuDisLevelMin,
  occuDisLevelArr: (state) => state.occuDisLevelArr,
  hasResidentFile: (state) => state.hasResidentFile,
  revenueFileType: (state) => state.revenueFileType,
  hasApiCheckAddr: (state) => state.hasApiCheckAddr,
  benefyOrderMax: (state) => state.benefyOrderMax,
  benefyNumMax: (state) => state.benefyNumMax,
  isFiristBill: (state) => state.isFiristBill,
  firstBillRelateCode: (state) => state.firstBillRelateCode,
  fieldDescConfig: (state) => state.fieldDescConfig,
  limitFeeObj: (state) => state.limitFeeObj,
  riskRelateOccup: (state) => state.riskRelateOccup,

  fxqCount: (state) => state.fxqCount,
  preInputFlag: (state) => state.preInputFlag,
  isPrerecordOrder: (state) => state.isPrerecordOrder,
  preRecordOrderMsg: (state) => state.preRecordOrderMsg,

  healthyLists: (state) => state.healthyLists,
  preNotice: (state) => state.preNotice,
  autoHealthyApi: (state) => state.autoHealthyApi,
  riskRelateNotice: (state) => state.riskRelateNotice,
  minorCustodyCertificate: (state) => state.minorCustodyCertificate,
  isNetInsure: (state) => state.isNetInsure,
  riskCheckNotice: (state) => state.riskCheckNotice,
  bbrAutoHealthApi: (state) => state.bbrAutoHealthApi,
  noticeConfigType: (state) => state.noticeConfigType,

  medicalPhotos: (state) => state.medicalPhotos,
  bnfPhotos: (state) => state.bnfPhotos,
  selfiePhotos: (state) => state.selfiePhotos,
  specialEntity: (state) => state.specialEntity,
  bankPhoto: (state) => state.bankPhoto,

  doubRecordAbutment: (state) => state.doubRecordAbutment,
  doubRecordForbid: (state) => state.doubRecordForbid,
  dRecordPassFlag: (state) => state.dRecordPassFlag,
  doubleRecordEwmApi: (state) => state.doubleRecordEwmApi,
  drEwmImage: (state) => state.drEwmImage,
  showCashTable: (state) => state.showCashTable,
  checkCashRead: (state) => state.checkCashRead,

  dlrDontSign: (state) => state.dlrDontSign,
  interLinkSign: (state) => state.interLinkSign,
  singleFileSign: (state) => state.singleFileSign,
  fileSignUls: (state) => state.fileSignUls,
  transcribeDesc: (state) => state.transcribeDesc,
  transcribeText: (state) => state.transcribeText,
  transcribeProducts: (state) => state.transcribeProducts,
  targetsModalName: (state) => state.targetsModalName,

  ocrFaceVerify: (state) => state.ocrFaceVerify,
  dontRepalceBank: (state) => state.dontRepalceBank,
  dontChangePayMode: (state) => state.dontChangePayMode,
  revisitBtnLink: (state) => state.revisitBtnLink,
  revisitDomain: (state) => state.revisitDomain,
  tgForInsureLink: (state) => state.tgForInsureLink,
  tgNoneInsuAgent: (state) => state.tgNoneInsuAgent,
  ruleWarn: (state) => state.ruleWarn,
  insuYear: (state) => state.insuYear,
  renewal: (state) => state.renewal,
  showXbModal: (state) => {
    let res = false; // 网销
    if (
      ["1", "3"].includes(state.isNetInsure) &&
      Object.keys(state.renewal).length > 0
    ) {
      res = true;
    }
    return res;
  },
  showXbCount: (state) => {
    let res = false; // 百年主、附一年期
    if (state.isNetInsure == "2" && Object.keys(state.renewal).length > 0) {
      res = true;
    }
    return res;
  },
  noticeMode: (state) => state.noticeMode,
  clickReadMode: (state) => state.clickReadMode,
};

const mutations = {
  SET_PAGE_CONFIG(state, payload) {
    Object.assign(state, payload);
  },
  // 合并字段
  MERGE_STATE_CONFIG(state, payload) {
    for (const key in payload) {
      state[key] = payload[key];
    }
  },
  SET_YULU_FLAG(state, payload) {
    state.isPrerecordOrder = payload;
  },
  SET_YULU_MSG(state, payload) {
    state.preRecordOrderMsg = payload;
  },
  SET_DR_IMAGE(state, payload) {
    state.drEwmImage = payload;
  },
  SET_DR_RESULT(state, payload) {
    state.dRecordPassFlag = payload;
  },
  SET_INDEX_GZ_FLAG(state, payload) {
    state.gzPopRead = payload;
  },
  SET_FIELD_DESC_CFG(state, payload) {
    state.fieldDescConfig = payload;
  },
};
const actions = {
  initPageConfig(ctx, data) {
    // console.log("page/initPageConfig", data);
    for (const key in data) {
      /* === 通用码表配置 === */
      // 1.告知
      if (key === "healthys" && data.healthys.length > 0) {
        let lists = data.healthys.map((item) => {
          const fdObj = pageDits.healthyList.find((ele) => ele.type === item);
          return fdObj;
        });
        ctx.commit("SET_PAGE_CONFIG", {
          healthyLists: lists,
        });
      } else if (key === "inputReg" && Object.keys(data.inputReg).length > 0) {
        if (data.inputReg.insured) {
          ctx.commit("infoReg/UPDATE_BBR_OBJ", data.inputReg.insured, {
            root: true,
          });
          ctx.commit("insert/SET_MULT_BBR_TRG", data.inputReg.insured, {
            root: true,
          });
        }
        if (data.inputReg.benefitModel) {
          ctx.commit("infoReg/UPDATE_BNF_OBJ", data.inputReg.benefitModel, {
            root: true,
          });
        }
        if (data.inputReg.applicant) {
          ctx.commit("infoReg/UPDATE_TBR_OBJ", data.inputReg.applicant, {
            root: true,
          });
        }
        if (data.inputReg.payInfo) {
          ctx.commit("infoReg/UPDATE_BILL_OBJ", data.inputReg.payInfo, {
            root: true,
          });
        }
        if (data.inputReg.targetModel) {
          ctx.commit("infoReg/UPDATE_TARGET_OBJ", data.inputReg.targetModel, {
            root: true,
          });
        }
      } else {
        if (!["healthyLists"].includes(key)) {
          let obj = {};
          obj[key] = data[key];
          ctx.commit("MERGE_STATE_CONFIG", obj);
        }
      }
    }
  },
  initPreOrderConfig({ commit, state, rootState }) {
    const configObj = state.preInputFlag;
    if (!configObj.minTime) return;
    const bbrObj = rootState.insert.bbrMultList[0] || {},
      proCode = rootState.insert.mainRiskCode;
    // 1. 生日单判断
    let isBirthBill = false,
      res = false,
      msg = "";
    if (configObj.birthDayOrder) {
      const checkObj = configObj.birthDayOrder,
        checkPros = checkObj.pros,
        cond1 = checkPros.includes(proCode);
      if (cond1 && yuLuBirthOrder(bbrObj.birthDate)) {
        isBirthBill = true;
      }
    }
    // 2. 判断
    if (configObj.minTime) {
      const curTime = new Date().getTime(),
        preInputPros = configObj.proCode,
        minVal = configObj.minTime,
        maxVal = configObj.maxTime,
        cond2 = preInputPros.includes(proCode),
        rightTime = curTime > minVal && curTime < maxVal;
      if (cond2 && rightTime) {
        res = true;
        msg = configObj.tipDesc;
        // 生日件客户不支持年金险的预录单投保
        if (isBirthBill) {
          res = false;
          if (configObj.birthDayOrder.tipDesc) {
            msg = configObj.birthDayOrder.tipDesc;
          }
        }
      }
    }
    commit("SET_YULU_FLAG", res);
    commit("SET_YULU_MSG", msg);
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
